cap log close
cap log using ///
	"D:/Files/Shasta County/CarlyWill_Scratch/analysis.text", t replace 
/*****************************************************************************/	
dis as error "{hline 59}" _newline ///
	as text "Ran by: " 	_column(16) c(username) 		_newline ///
	as text "Date: " 	_column(16) c(current_date) 	_newline ///
	as text "Time: " 	_column(16) c(current_time) 	_newline ///
	as error "{hline 59}"
/****************************************************************************
// Title: 	analysis.do 
// Where? 	"D:/Files/Shasta County/CarlyWill_Scratch"
// by: 		CW		
// Purpose: Running  regressions
				
// Edits/To-Do/Comments:
	- [MM/DD/YY] - [name] ...
------------------------------------------------------------------------------
TOC
------------------------------------------------------------------------------
// PART 1: Balance Table

	// iebaltab `characs' , grpvar(group)
	
// PART 2: ITT + Rand. Inf. + Boottest
	
	// reg treated d if homeless==`i',r
	// ritest d _b[d], seed(999): `e(cmdline)'
	// boottest d, seed(999) nograph 
	
// Part 3: ITT X Male

	// reg fta_gnrm d_male assign_treatment male   if homeless==`i', r
	
// Part 4: ITT + TOT 

	// reg fta_gnrm assign_treatment if homeless==`i', r
	// reg treated assign_treatment if homeless==`i',r
	// ivregress 2sls fta_gnrm (treated=assign_treatment) if homeless== `i', r
*****************************************************************************/
set more off, perm

global dir "D:/Files/Shasta County"
global data "D:/Files/Shasta County/CarlyWill_Scratch/Data"
global temp "D:/Files/Shasta County/CarlyWill_Scratch/_temp"
global output "D:/Files/Shasta County/CarlyWill_Scratch/Output"
adopath + "D:/Files/Shasta County/ado"

global cntrl ///
	  ///
	 max_prev_case max_prev_fta 
	 
global cntrl_interact ///
	  ///
	 max_prev_case max_prev_fta 
	 
global baltab ///
	age male shasta_code  white ///
	californian phone_miss max_prev_case max_prev_fta prev_case_cons prev_fta_cons
	

use  "${data}/_RCT_Sample.dta", clear

	sort person_key court_dateD
	by person_key , sort: keep if _n == 1
	
/***********************************************************************/
/* PART 1: Balance Table */
/***********************************************************************/
use  "${data}/_RCT_Sample.dta", clear

	sort person_key court_dateD
	by person_key , sort: keep if _n == 1

	
	
	egen group = group(homeless assign_treatment) 

gen phone_miss = phone == "0"
la var phone_miss "Missing Phone Number"

	
# d ; 
iebaltab $baltab , grpvar(group) 
	rowv starlevels(0.05 0.01 0.001) ftest pboth notecombine 
	grplabels(
		"1 Housed Control @
		2 Housed Treatment @
		3 Unhoused Control @
		4 Unhoused Treatment")
	total totallabel("Total")
	onerow
	save("${output}/T_baltable.xlsx") replace;

# d cr	

	
# d ; 
iebaltab $baltab if phone_miss==1, grpvar(group) 
	rowv starlevels(0.05 0.01 0.001) ftest pboth notecombine 
	grplabels(
		"1 Housed Control @
		2 Housed Treatment @
		3 Unhoused Control @
		4 Unhoused Treatment")
	total totallabel("Total")
	onerow
	save("${output}/T_baltable_nophone.xlsx") replace;

# d cr	

	
# d ; 
iebaltab $baltab if phone_miss==0, grpvar(group) 
	rowv starlevels(0.05 0.01 0.001) ftest pboth notecombine 
	grplabels(
		"1 Housed Control @
		2 Housed Treatment @
		3 Unhoused Control @
		4 Unhoused Treatment")
	total totallabel("Total")
	onerow
	save("${output}/T_baltable_phone.xlsx") replace;

# d cr	

/***********************************************************************/
/* Part 2: ITT   */
/***********************************************************************/


local se ///
	r
	
estimates clear
local m=1

forval i=0/1 {
/*****************************************/
/* 2.A:  NO CONTROL */
/*****************************************/
	/* just for table exporting: */
	cap drop d 
	gen d = assign_treatment
	/*-------------------------- */	
reg treated d if homeless==`i',`se'
	local bfs = _b[d]
	
reg fta_gnrm d if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)
	
/* RI */
/*---------------------------*/
ritest d _b[d], seed(999): `e(cmdline)'
	mat ri_ps=r(p)
	estimates store t_`m', title("ITT - HL==`i'")
	 local ri_p = ri_ps[1,1] 
	
	su fta_any if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar ri_p = `ri_p'
	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "."
	estadd local crim_cntrl "."
	
		local m `++m'
		dis `m'
/*****************************************/
/* 2.B:  DATE FE */
/*****************************************/
reg fta_gnrm d i.court_dateD if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)

/* RI */
/*---------------------------*/
ritest d _b[d], seed(999): `e(cmdline)'
	mat ri_ps=r(p)
	estimates store t_`m', title("ITT - HL==`i'")
	 local ri_p = ri_ps[1,1] 

	su fta_any if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar ri_p = `ri_p'
	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "."

		local m `++m'
		dis `m'
	
/*****************************************/
/* 2.C:   CONTROLS */
/*****************************************/
reg fta_any d  $cntrl if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)
	
/* RI */
/*---------------------------*/
ritest d _b[d], seed(999): `e(cmdline)'
	mat ri_ps=r(p)
	estimates store t_`m', title("ITT - HL==`i'")
	 local ri_p = ri_ps[1,1] 
	 
	su fta_any if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar ri_p = `ri_p'
	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "."
	estadd local crim_cntrl "Y"
	
	 	local m `++m'
		dis `m'
/*****************************************/
/* 2.D:  DATE FE & CONTROLS */
/*****************************************/
reg fta_gnrm d i.court_dateD $cntrl if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)
	
/* RI */
/*---------------------------*/
ritest d _b[d], seed(999): `e(cmdline)'
	mat ri_ps=r(p)
	estimates store t_`m', title("ITT - HL==`i'")
	 local ri_p = ri_ps[1,1] 
	
	su fta_any if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar ri_p = `ri_p'
	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "Y"
		local m `++m'
		dis `m'
}	
la var d "Exp vs Control"
# d ; 
esttab t_* using "${output}/T_results_gnrm7.csv", replace 
	scalar(
		"ri_p Rand Inf p-value"
		"date_cntrl Date FE"
		"crim_cntrl Crim History Controls"
		"r2a Adjusted R2"
		"bfs Take-Up"
		"cntrl_mean Control Mean"
		"obs N"
		)
	noobs
	keep(d)
	mlabels(, titles)
	collabels(none)
	starlevels(* 0.1 ** 0.05 *** 0.01)
	cells((b(star fmt(%9.3f))) (se(fmt(%9.3f)par)))
	label varlabels(`e(labels)') 
	title(ITT)
	;
# d cr	

cap drop d

/*************************************************/
/* PART 3: ITT  Interaction */
/*************************************************/

la var assign_treatment "ITT"
gen d_male = assign_treatment*male 
la var d_male "ITT X Male"

la var male "Male"
 
local d ///
	d_male assign_treatment male
	
estimates clear
local m=1

forval i=0/1 {
/*****************************************/
/* 3.A:  NO CONTROL */
/*****************************************/
	/* just for table exporting: */
	/*-------------------------- */	
reg treated assign_treatment if homeless==`i',`se'
	local bfs = _b[assign_treatment]
	
reg fta_gnrm `d'   if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)

	
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "."
	estadd local crim_cntrl "."
		

		local m `++m'
		dis `m'
/*****************************************/
/* 3.B:  DATE FE */
/*****************************************/
reg fta_gnrm `d' i.court_dateD if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)

	
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 

	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "."
		local m `++m'
		dis `m'		
/*****************************************/
/* 3.C:  DATE FE + CONTROL */
/*****************************************/	
reg fta_gnrm `d' i.court_dateD $cntrl_interact if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")

	local obs = e(N)
	local r2a = round(e(r2_a), 0.001)

	
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	
	estadd scalar bfs = `bfs'
	estadd scalar obs = `obs'
	estadd scalar r2a = `r2a'
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "Y"

		local m `++m'
		dis `m'
}
	

# d ; 
esttab t_* using "${output}/T_results_gnrm7.csv", append 
	scalar(
		"date_cntrl Date FE"
		"crim_cntrl Crim History Controls"
		"r2a Adjusted R2"
		"bfs Take-Up"
		"cntrl_mean Control Mean"
		"obs N"
		)
	noobs
	keep(`d')
	mlabels(, titles)
	collabels(none)
	starlevels(* 0.1 ** 0.05 *** 0.01)
	cells((b(star fmt(%9.3f))) (se(fmt(%9.3f)par)))
	label varlabels(`e(labels)') 
	title(ITT X Gender)
	;
# d cr	

/***********************************************************************/
/* PART 4: ITT & TOT */
/***********************************************************************/
	 
estimates clear
local m=1


forval i=0/1 {
/*****************************************/
/* 4.A:  NO CONTROL */
/*****************************************/
	/* just for table exporting: */
	cap drop d 
	gen d = assign_treatment
	/*-------------------------- */	
reg fta_gnrm d if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")
	
	estadd scalar obs = e(N)
	estadd scalar r2a = e(r2_a)
	estadd local date_cntrl "."
	estadd local crim_cntrl "."
	 
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
		local m `++m'
		dis `m'

/* FS */
/*-------------------------- */
reg treated assign_treatment if homeless==`i',`se'

	local bfs = _b[assign_treatment]
	local sefs = _se[assign_treatment]

	unab ivs: assign_treatment
	local xlist: colnames e(b)
	local ivs: list ivs & xlist 
	test `ivs' 
	local F_fs = r(F)
	
/*  TOT */
/*-------------------------- */	
	/* just for table exporting: */
	cap drop d 
	gen d = treated
	/*-------------------------- */
ivregress 2sls fta_gnrm (d=assign_treatment) if homeless== `i', `se'
	 	
	estimates store t_`m', title("TOT - HL==`i'")
	
	estadd scalar obs = e(N)
	estadd scalar r2a = round(e(r2_a), 0.001)
	 
	su fta_gnrm if homeless ==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar bfs = `bfs'
	estadd scalar sefs = `sefs'
	estadd scalar F_fs = `F_fs'
	estadd local date_cntrl "."
	estadd local crim_cntrl "."

	
		local m `++m'
		dis `m'	
		
/*****************************************/
/* 4.B: DATE FE */
/*****************************************/	
	/* just for table exporting: */
	cap drop d 
	gen d = assign_treatment
	/*-------------------------- */	
reg fta_gnrm d i.court_dateD if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")
	
	estadd scalar obs = e(N)
	estadd scalar r2a = e(r2_a)
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "."
	 
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
		local m `++m'
		dis `m'
/*  FS */
/*-------------------------- */
reg treated assign_treatment i.court_dateD if homeless==`i',`se'

	local bfs = _b[assign_treatment]
	local sefs = _se[assign_treatment]

	unab ivs: assign_treatment
	local xlist: colnames e(b)
	local ivs: list ivs & xlist 
	test `ivs' 
	local F_fs = r(F)
	
/*  TOT */
/*-------------------------- */	
	/* just for table exporting: */
	cap drop d 
	gen d = treated
	/*-------------------------- */
ivregress 2sls fta_gnrm (d=assign_treatment) i.court_dateD if homeless==`i', `se'
	 	
	estimates store t_`m', title("TOT - HL==`i'")
	
	 estadd scalar obs = e(N)
	 estadd scalar r2a = e(r2_a)
	 
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar bfs = `bfs'
	estadd scalar sefs = `sefs'
	estadd scalar F_fs = `F_fs'
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "."
	
		local m `++m'
		dis `m'
		
/*****************************************/
/* 4.C: DATE & CRIM CONTROL */
/*****************************************/	
	/* just for table exporting: */
	cap drop d 
	gen d = assign_treatment
	/*-------------------------- */	
reg fta_gnrm d i.court_dateD $cntrl if homeless==`i', `se'
	
	estimates store t_`m', title("ITT - HL==`i'")
	
	estadd scalar obs = e(N)
	estadd scalar r2a = e(r2_a)
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "Y"
	 
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
		local m `++m'
		dis `m'
/*  FS */
/*-------------------------- */
reg treated assign_treatment i.court_dateD  $cntrl if homeless==`i',`se'

	local bfs = _b[assign_treatment]
	local sefs = _se[assign_treatment]

	unab ivs: assign_treatment
	local xlist: colnames e(b)
	local ivs: list ivs & xlist 
	test `ivs' 
	local F_fs = r(F)
	
/* TOT  */
/*-------------------------- */	
	/* just for table exporting: */
	cap drop d 
	gen d = treated
	/*-------------------------- */
ivregress 2sls fta_gnrm (d=assign_treatment) i.court_dateD $cntrl if homeless==`i', `se'
	 	
	estimates store t_`m', title("TOT - HL==`i'")
	
	 estadd scalar obs = e(N)
	 estadd scalar r2a = e(r2_a)
	 
	su fta_gnrm if homeless==`i' & assign_treatment == 0
	local cntrl_mean = r(mean)
	estadd scalar cntrl_mean = `cntrl_mean' 
	
	estadd scalar bfs = `bfs'
	estadd scalar sefs = `sefs'
	estadd scalar F_fs = `F_fs'
	estadd local date_cntrl "Y"
	estadd local crim_cntrl "Y"
	
		local m `++m'
		dis `m'
}
	
la var d "Exp vs Control"
# d ; 
esttab t_* using "${output}/T_results_gnrm7.csv", append 
	scalar(
		"bfs First Stage Coeff."
		"sefs (se)"
		"F_fs First Stage F Statistics"
		"date_cntrl Date FE"
		"crim_cntrl Crim History Controls"
		"r2a Adjusted R2"
		"cntrl_mean Control Mean"
		"obs N"
		)
	noobs
	keep(d)
	mlabels(, titles)
	collabels(none)
	starlevels(* 0.1 ** 0.05 *** 0.01)
	cells((b(star fmt(%9.3f))) (se(fmt(%9.3f)par)))
	label varlabels(`e(labels)') 
	title(ITT - TOT)
	;
# d cr	

/***********************************************************************/
/* PART 1: Means Graph */
/***********************************************************************/
use  "${data}/_RCT_Sample.dta", clear

	sort person_key court_dateD
	by person_key , sort: keep if _n == 1
	
egen group = group(homeless assign_treatment) 

collapse (mean) meanfta=fta_any group (sd) sdfta=fta_any (count) n=fta_any, by(homeless assign_treatment)
g hi=meanfta+invttail(n-1,0.025)*((sdfta/sqrt(n)))
g low=meanfta-invttail(n-1,0.025)*((sdfta/sqrt(n)))

format meanfta %9.2f
twoway (bar meanfta group if group==1, color(gray)) ///
	(scatter meanfta group, mlabel(meanfta) mlabposition(2) msymbol(none)) ///
	(bar meanfta group if group==2, color(black)) /// 
	(bar meanfta group if group==3, color(gray)) /// 
	(bar meanfta group if group==4, color(black)) ///
	(rcap hi low group), ///
	legend(off) ytitle(Failure to Appear) xtitle(" ") ///
	xlabel(1.5 "Housed" 3.5 "Unhoused") text(.42 2 "Treatment" .42 4 "Treatment", color(white))

graph export "${output}/means.eps", replace

twoway (bar meanfta group if group==1, color(gray)) ///
	(scatter meanfta group if group==1 | group==2, mlabel(meanfta) mlabposition(2) msymbol(none) mlabcolor(black)) ///
	(bar meanfta group if group==2, color(black)) /// 
	(rcap hi low group) if group==1 | group==2, ///
	legend(off) ytitle(Failure to Appear) xtitle(" ") ///
	xlabel(1.5 "Housed") text(.42 2 "Treatment", color(white)) ///
	yscale(r(0(0.2)1)) ylabel(0(0.2)1)
	 graph export "${output}/meansHF.eps", replace
	
twoway (bar meanfta group if group==3, color(gray)) ///
	(scatter meanfta group if group==3 | group==4, mlabel(meanfta) mlabposition(2) msymbol(none) mlabcolor(black)) ///
	(bar meanfta group if group==4, color(black)) /// 
	(rcap hi low group) if group==3 | group==4, ///
	legend(off) ytitle(Failure to Appear) xtitle(" ") ///
	xlabel(3.5 "Unhoused") text(.42 2 "Treatment" , color(white)) ///
	yscale(r(0(0.2)1)) ylabel(0(0.2)1)
	 graph export "${output}/meansUF.eps", replace
	 
twoway (bar meanfta group if group==1, color(gray)) ///
	(scatter meanfta group if group==1 | group==2, mlabel(meanfta) mlabposition(2) msymbol(none) mlabcolor(black)) ///
	(bar meanfta group if group==2, color(black)) /// 
	(rcap hi low group) if group==1 | group==2, ///
	legend(off) ytitle(Failure to Appear) xtitle(" ") ///
	xlabel(1.5 "Housed") text(.37 2 "Treatment", color(white)) ///
	yscale(r(.35(0.05).55)) ylabel(.35(0.05).55)
	 graph export "${output}/meansHsmall.eps", replace
	
twoway (bar meanfta group if group==3, color(gray)) ///
	(scatter meanfta group if group==3 | group==4, mlabel(meanfta) mlabposition(2) msymbol(none) mlabcolor(black)) ///
	(bar meanfta group if group==4, color(black)) /// 
	(rcap hi low group) if group==3 | group==4, ///
	legend(off) ytitle(Failure to Appear) xtitle(" ") ///
	xlabel(3.5 "Unhoused") text(.82 4 "Treatment" , color(white)) ///
	yscale(r(.8(0.05)1)) ylabel(.8(0.05)1)
	 graph export "${output}/meansUsmall.eps", replace	 
/*
/******************************/
/* PART 3: DiD */
/******************************/
estimates clear
local m=1

reg fta_gnrm did assign_treatment homeless, vce(cluster person_key)
	
	estimates store t_`m', title("DiD_clustered")
	
	estadd scalar obs = e(N)
	estadd scalar r2a = e(r2_a)
	
	local m `++m'
/*--------------------------------------------*/
		sort person_key court_dateD
		by person_key , sort: keep if _n == 1
/*--------------------------------------------*/

reg fta_gnrm did assign_treatment homeless, r
	
	estimates store t_`m', title("DiD_Robust")
	
	estadd scalar obs = e(N)
	estadd scalar r2a = e(r2_a)
	
	local m `++m'

# d ; 
esttab t_* using "${dir}/CarlyWill_Scratch/T_results_gnrm7.csv", append 
	r2
	drop(_cons)
	collabels(none)
	mlabels(, titles)
	starlevels(* 0.1 ** 0.05 *** 0.01)
	cells((b(star fmt(%9.3f))) (se(fmt(%9.3f)par)))
	label varlabels(`e(labels)') 
	title(TITLE: DiD)
	addnotes(DiD);
# d cr	







